package cn.sc.summer.job.runner;

import cn.sc.summer.job.config.XxlJobExecutorX;
import cn.sc.summer.job.service.JobService;
import com.xxl.job.core.executor.XxlJobExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;

/**
 * 类名：JOB启动类
 *
 * @author a-xin
 * @date 2023/8/25 14:31
 */
@Slf4j
@Component
public class JobRunner implements CommandLineRunner, ApplicationListener<ContextClosedEvent> {

    @Resource
    private JobService jobService;

    @Override
    public void run(String... args) throws NoSuchMethodException, UnsupportedEncodingException {
//        jobService.registerServerGroup();
        log.info("==> The job has been initialized！");
    }

    @Override
    public void onApplicationEvent(@NonNull ContextClosedEvent event) {
        XxlJobExecutor xxlJobExecutor = new XxlJobExecutorX();
        xxlJobExecutor.destroy();
        jobService.refreshServerGroup();
    }

}
